package com.zaoke.web.component.controller;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zaoke.web.bo.query.user.UserQuery;
import com.zaoke.web.bo.user.User;
import com.zaoke.web.service.user.UserService;
import com.zaoke.web.utils.JacksonUtil;
import com.zaoke.web.utils.WebResponse;

@Controller
@RequestMapping("/user/")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@ResponseBody
	@RequestMapping(value="queryUser.ajax",method = RequestMethod.POST)  
    public WebResponse<ArrayList<User>> queryUser(HttpServletRequest request, HttpServletResponse response) {
		
		WebResponse<ArrayList<User>> wr = new WebResponse<ArrayList<User>>();
		UserQuery query;
		try {
			query = JacksonUtil.readObj(request.getInputStream(), UserQuery.class);
			List<User> users = userService.queryUserInfo(query);
			
			if(users != null && users.size() > 0)
				for(User user : users){
					user.setBalanceStr(new DecimalFormat("0.00").format(user.getBalance()));
					user.setConsumeStr(new DecimalFormat("0.00").format(user.getConsume()));
				}
			
			ArrayList<User> dest = new ArrayList<User>(users);
			wr.setData(dest);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return wr;
    }

//	public void setUserService(UserService userService) {
//		this.userService = userService;
//	}
}
